#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import json

from tpops.gaussdb.common.log import log
from common.parse_parafile import ParamFileUtil
from validation.common.json_util import find_all_value_by_key
from validation.validator import ParamValidator


def tpops_parse_param_with_jsonschema(pid):
    """
    func: parse_param_with_jsonschema()
    参数校验，校验"common schema", "tpops schema", 和额外的"agent_application schema"
    :param pid: 任务pid: int
    :return file_content: json string
    """
    path = "tpops/gaussdb/jsonschema/tpops_base_define.json"
    tpops_agent_application_jsonschema_path = "tpops/gaussdb/jsonschema/tpops_agent_applications_define.json"

    file_content = ParamFileUtil.parse_param_file_and_valid_by_schema(pid, path)
    agent_application_list = find_all_value_by_key(file_content, "agent_applications")
    for agent_application in agent_application_list:
        ParamValidator.valid_data_by_schema(json.loads(agent_application), tpops_agent_application_jsonschema_path)

    log.info("Param is valid!")
    return file_content
